package com.ruoyi.mq.service.serviceImpl.Processor;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.mq.service.MessageProcessor;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 处理产品信息类
 */
@Component
public class UserInfoProcessor implements MessageProcessor {


    @Autowired
    private ISysUserService userService;
    @Transactional
    public void processMessage(JSONObject message) {
        // 处理公司信息

        String tag = message.getString("tag");
        //获取对象数据
        String data = message.getString("data");
        // 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();
        switch (tag){
            //新增企业和节点
            case "add":
                try {
                    // 转换为自定义对象
                    SysUser sysUser = objectMapper.readValue(data, SysUser.class);
                    //创建对象
                    SysUser user = new SysUser();
                    //拷贝对象到节点表
                    BeanUtils.copyProperties(sysUser, user);
//                    //设置节点标识符
//                    sysDept.setNodeIdentifier(message.getString("niot"));
                    //新增节点数据
                    userService.insertUser(user);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                break;
            //删除企业和节点
            case "delete":
                //批量删除
                String[] parts = data.replaceAll("[\\[\\]]", "").split(",");

                for (String part : parts) {
                    //删除用户
                    userService.deleteUserById(Long.valueOf(part));
                }
                break;
            //修改企业和节点
            case "update":

                try {
                    // 转换为自定义对象
                    SysUser sysUser = objectMapper.readValue(data, SysUser.class);
                    //创建对象
                    SysUser user = new SysUser();
                    //拷贝对象到节点表
                    BeanUtils.copyProperties(sysUser, user);
//                    //设置节点标识符
//                    sysDept.setNodeIdentifier(message.getString("niot"));
                    //新增节点数据
                    userService.updateUser(user);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                break;
            default:
                break;
        }


    }

}
